package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.mapapi.search.core.PoiInfo;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.ui.holder.SearchSiteHolder;

import java.util.List;

/**
 * Created by apple on 2017/4/8.
 */

public class SearchSiteAdapter extends RecyclerView.Adapter<SearchSiteHolder> {
    private Context context;

    public void setSiteList(List<PoiInfo> siteList) {
        this.siteList = siteList;
        notifyDataSetChanged();
    }
    public void addSiteList(List<PoiInfo> siteList) {
        this.siteList.addAll(siteList);
        notifyDataSetChanged();
    }

    private List<PoiInfo> siteList;
    private OnItemClickListener mOnItemClickListener;


    public SearchSiteAdapter(Context context, List<PoiInfo> siteList) {
        this.context = context;
        this.siteList = siteList;
    }


    @Override
    public SearchSiteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SearchSiteHolder(View.inflate(context, R.layout.item_addr_layout, null));

    }

    @Override
    public void onBindViewHolder(SearchSiteHolder holder, final int position) {
        holder.setData(siteList, position);
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(siteList.get(position));
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(siteList.get(position));
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return siteList == null ? 0 : siteList.size();
    }

    public interface OnItemClickListener {
        void onClick(PoiInfo poiInfo);

        void onLongClick(PoiInfo poiInfo);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
